package org.secangkirkopipanas.gxtcore.client;

import org.secangkirkopipanas.gxtcore.domain.ILoginResult;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginForm extends LayoutContainer {

	private VerticalPanel vp;

	public LoginForm() {
		System.currentTimeMillis();
		setLayout(new CenterLayout());
		vp = new VerticalPanel();
		vp.setSpacing(10);

		createLoginForm();

		add(vp);
	}

	private void createLoginForm() {
		FormPanel fp = new FormPanel();
		fp.setHeading("Login Form");
		fp.setFrame(true);
		fp.setWidth(350);

		final TextField<String> tfUsername = new TextField<String>();
		tfUsername.setFieldLabel("Username");
		tfUsername.setAllowBlank(false);
		tfUsername.setToolTip("Username");
		tfUsername.setMinLength(8);
		tfUsername.setMaxLength(50);
		fp.add(tfUsername);

		final TextField<String> tfPassword = new TextField<String>();
		tfPassword.setFieldLabel("Password");
		tfPassword.setAllowBlank(false);
		tfPassword.setPassword(true);
		tfPassword.setToolTip("Password");
		tfPassword.setMinLength(8);
		tfPassword.setMaxLength(50);
		fp.add(tfPassword);

		Button btnLogin = new Button("Login");
		btnLogin.addSelectionListener(new SelectionListener<ComponentEvent>() {

			@Override
			public void componentSelected(ComponentEvent ce) {
				Info.display("Login Event",
						"Please wait for processing the user data...");

				GxtCoreRemoteService.Util.getInstance().login(
						tfUsername.getValue(), tfPassword.getValue(),
						new AsyncCallback<ILoginResult>() {
							public void onFailure(Throwable caught) {
								Info.display("Error", caught.getMessage());
								com.google.gwt.user.client.Window.alert(caught
										.getMessage());
								caught.printStackTrace();
							}

							public void onSuccess(ILoginResult result) {
								if (result != null) {
									// SystemUserDTO systemUser =
									// (SystemUserDTO) result
									// .getSystemUser();
									// Info
									// .display(
									// "Info",
									// "Anda telah login menggunakan username: "
									// + systemUser
									// .getFirstname());
								} else {
									com.google.gwt.user.client.Window
											.alert("Invalid username or password");
									Info.display("Info",
											"Invalid username or password");
								}
							}
						});
			}

		});
		fp.addButton(btnLogin);

		Button btnReset = new Button("Reset");
		fp.addButton(btnReset);

		vp.add(fp);
	}

}